package com.java2108.file.controller;


import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import com.bw.zhanggm.common.AjaxResult;
import com.java2108.file.dto.ChunkDto;
import com.java2108.file.entity.Files;
import com.java2108.file.service.IFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 文件信息表 前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2024-02-23
 */
@RestController
@RequestMapping("/file")
public class FilesController {

    @Autowired
    FileService fileService;

    @Autowired
    private IFilesService iFilesService;


    @PostMapping("upload")
    public AjaxResult upload(@ModelAttribute ChunkDto chunk) throws InterruptedException {
        fileService.postFileUpload(chunk);
        if (chunk.getChunkNumber().equals(chunk.getTotalChunks())) {
            TimeUnit.SECONDS.sleep(5);
            FileResult fileResult = fileService.mergeFile(chunk);
            //入库
            Files files = new Files().setHtId(Long.valueOf(chunk.getHetongId())).setFileName(fileResult.getFileUrl());
            iFilesService.save(files);
            
            return AjaxResult.success(fileResult);
        }
        return AjaxResult.success();
    }

}
